package org.zwy.gradetrack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.zwy.gradetrack.entity.Employer;
import org.zwy.gradetrack.mapper.EmployerMapper;
import org.zwy.gradetrack.service.EmployerService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用人单位服务实现类
 */
@Service
public class EmployerServiceImpl extends ServiceImpl<EmployerMapper, Employer> implements EmployerService {

    @Override
    public Page<Employer> pageEmployers(Page<Employer> page, String name) {
        LambdaQueryWrapper<Employer> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.like(Employer::getName, name);
        }
        wrapper.orderByDesc(Employer::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createEmployer(Employer employer) {
        // 检查单位名称是否已存在
        LambdaQueryWrapper<Employer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employer::getName, employer.getName());
        if (count(wrapper) > 0) {
            throw new RuntimeException("单位名称已存在");
        }
        
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        employer.setCreateTime(now);
        employer.setUpdateTime(now);
        
        return save(employer);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEmployer(Employer employer) {
        // 检查单位是否存在
        Employer existingEmployer = getById(employer.getId());
        if (existingEmployer == null) {
            throw new RuntimeException("用人单位不存在");
        }
        
        // 检查单位名称是否与其他单位重复
        if (!existingEmployer.getName().equals(employer.getName())) {
            LambdaQueryWrapper<Employer> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Employer::getName, employer.getName());
            if (count(wrapper) > 0) {
                throw new RuntimeException("单位名称已存在");
            }
        }
        
        // 设置更新时间
        employer.setUpdateTime(LocalDateTime.now());
        
        return updateById(employer);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEmployer(Long id) {
        // 检查单位是否存在
        if (getById(id) == null) {
            throw new RuntimeException("用人单位不存在");
        }
        return removeById(id);
    }

    @Override
    public Employer getEmployerById(Long id) {
        Employer employer = getById(id);
        if (employer == null) {
            throw new RuntimeException("用人单位不存在");
        }
        return employer;
    }

    @Override
    public List<Employer> getEmployersByName(String name) {
        LambdaQueryWrapper<Employer> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Employer::getName, name);
        wrapper.orderByDesc(Employer::getCreateTime);
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchImport(List<Employer> employers) {
        if (employers == null || employers.isEmpty()) {
            throw new RuntimeException("导入数据不能为空");
        }
        
        // 检查单位名称是否重复
        for (Employer employer : employers) {
            LambdaQueryWrapper<Employer> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Employer::getName, employer.getName());
            if (count(wrapper) > 0) {
                throw new RuntimeException("单位名称已存在：" + employer.getName());
            }
        }
        
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        for (Employer employer : employers) {
            employer.setCreateTime(now);
            employer.setUpdateTime(now);
        }
        
        return saveBatch(employers);
    }
} 